<template>
  <a-modal
    title="编辑水電抄表記錄表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="房間名稱"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入房間名稱" v-decorator="['estateRoomName', {rules: [{required: true, message: '请输入房間名稱！'}]}]" />
        </a-form-item>
        <a-form-item
          label="租客"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入租客" v-decorator="['reservation', {rules: [{required: true, message: '请输入租客！'}]}]" />
        </a-form-item>
        <a-form-item
          label="類型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入類型" v-decorator="['type', {rules: [{required: true, message: '请输入類型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="類型名稱"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入類型名稱" v-decorator="['typeName', {rules: [{required: true, message: '请输入類型名稱！'}]}]" />
        </a-form-item>
        <a-form-item
          label="读数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="用量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="抄表日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择抄表日期" v-decorator="['meterReadingDate',{rules: [{ required: true, message: '请选择抄表日期！' }]}]" @change="onChangemeterReadingDate"/>
        </a-form-item>
        <a-form-item
          label="收租日"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择收租日" v-decorator="['rentedDay',{rules: [{ required: true, message: '请选择收租日！' }]}]" @change="onChangerentedDay"/>
        </a-form-item>
        <a-form-item
          label="费用"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label=" 抄表状态 初始读数 	填充到第1期账单 	读数被覆盖 	读数被覆盖 	换表"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入 抄表状态 初始读数 	填充到第1期账单 	读数被覆盖 	读数被覆盖 	换表" v-decorator="['meterStatus', {rules: [{required: true, message: '请输入 抄表状态 初始读数 	填充到第1期账单 	读数被覆盖 	读数被覆盖 	换表！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { hydropowerMeterEdit } from '@/api/modular/main/hydropowermeter/hydropowerMeterManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        meterReadingDateDateString: '',
        rentedDayDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              estateRoomName: record.estateRoomName,
              reservation: record.reservation,
              type: record.type,
              typeName: record.typeName,
              reading: record.reading,
              dosage: record.dosage,
              cost: record.cost,
              meterStatus: record.meterStatus
            }
          )
        }, 100)
        // 时间单独处理
        if (record.meterReadingDate != null) {
            this.form.getFieldDecorator('meterReadingDate', { initialValue: moment(record.meterReadingDate, 'YYYY-MM-DD') })
        }
        this.meterReadingDateDateString = moment(record.meterReadingDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.rentedDay != null) {
            this.form.getFieldDecorator('rentedDay', { initialValue: moment(record.rentedDay, 'YYYY-MM-DD') })
        }
        this.rentedDayDateString = moment(record.rentedDay).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.meterReadingDate = this.meterReadingDateDateString
            values.rentedDay = this.rentedDayDateString
            hydropowerMeterEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangemeterReadingDate(date, dateString) {
        this.meterReadingDateDateString = dateString
      },
      onChangerentedDay(date, dateString) {
        this.rentedDayDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
